package com.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * This class is responsible for retrieving pertinent data from web pages associated with the College of Engineering.
 * 
 * 		// Scraping College of Engineering 
		CollegeEngineeringWebScraper engineeringScraper = new CollegeEngineeringWebScraper();
		WebReader webReader = new WebReader(engineeringScraper);
		
		// Scrape About Us 
		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringAboutUs);
		webReader.scrapeWebPage(engineeringScraper.aboutUsUrl); 
		CollegeEngineeringWebData collegeData = engineeringScraper.webData; 
		List<String> aboutUsParagraphs = collegeData.aboutUsParagraphs; 
		    	
    	// Scrape Academics
    	engineeringScraper.setScraperType(ScraperType.CollegeEngineeringAcademics);
		webReader.scrapeWebPage(engineeringScraper.academicsUrl); 
		collegeData = engineeringScraper.webData; 
		Map<String, List<Link>> academicDepartments = collegeData.academicDepartmentDegrees;
		
		// Scrape Research Centers 
		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringResearchCenters);
		webReader.scrapeWebPage(engineeringScraper.researchCentersUrl); 
		collegeData = engineeringScraper.webData; 
		List<Link> researchCenters = collegeData.researchCenters;
		
		// Scrape Faculty Staff
		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringFacultyStaff);
		webReader.scrapeWebPage(engineeringScraper.facultyStaffUrl); 
		collegeData = engineeringScraper.webData; 
		Map<String, List<Contact>> facultyStaffInfo = collegeData.facultyStaffContactInfo;
		
 * @author micah.perry
 *
 */
public class CollegeEngineeringWebScraper extends WebScraper{
	/**
	 * The data object for the current instance of the CollegeEngineeringWebScraper.
	 */
	public CollegeEngineeringWebData webData = new CollegeEngineeringWebData();
	
	//TODO: Should we make these const values?
	public static String aboutUsUrl = "http://www.uah.edu/eng/about/about-us";
	public static String academicsUrl = "http://www.uah.edu/eng/about/academics";
	public static String researchCentersUrl = "http://www.uah.edu/eng/eng-research/research-centers";
	public static String facultyStaffUrl = "http://www.uah.edu/eng/faculty-staff";

	/**
	 * This method is responsible for scraping the correct webpages.
	 * @param targetDocument
	 */
	void scrapeWebPage(Document targetDocument) {
		switch(this.scraperType){
		case CollegeEngineeringAboutUs: 
			webData.aboutUsParagraphs = scrapeAboutUs(targetDocument);
			break;
		case CollegeEngineeringAcademics:
			webData.academicDepartmentDegrees = scrapeAcademicDepartments(targetDocument);
			break;
		case CollegeEngineeringResearchCenters:
			webData.researchCenters = scrapeResearchCenters(targetDocument);
			break;
		case CollegeEngineeringFacultyStaff:
			webData.facultyStaffContactInfo = scrapeFacultyStaff(targetDocument);
			break;
		default:
		}
	}
	
	private Map<String, List<Contact>> scrapeFacultyStaff(Document targetDocument) {
		Map<String, List<Contact>> returnFacultyStaffInfo = new HashMap<String, List<Contact>>();
		String email = "";
		
		String linkQuery = "h2";
		Elements departmentElements = targetDocument.select(linkQuery);

		for(Element department : departmentElements){

			System.out.println(department.text());
			
			Element table = department.nextElementSibling();	
			
			if(table == null)
			{
				while(table == null)
				{
					table = department.parent().nextElementSibling();
				}
			}
			
			if (table != null)
			{
				Elements contactElements = table.select("tr"); 
				
				List<Contact> contacts = new ArrayList<Contact>();
				
				for(Element contactElement : contactElements){
					
					Element childElementImage = contactElement.child(0);
					Element childElementTitle = contactElement.child(1);
					Element childElementContact = contactElement.child(2);

					String contactName = childElementTitle.select("strong").text();

					try{
						email = childElementContact.select("a").first().text();
					}catch(Exception ex){
						email = "";
					}
					
					String phoneNumber = childElementContact.childNodes().get(0).outerHtml();
						
					String pictureUrl = childElementImage.select("img").first().absUrl("src");
					Link pictureLink = new Link(pictureUrl, contactName); 
					
					Contact contact = new Contact(contactName, email, phoneNumber, pictureLink);
					contacts.add(contact); 
				}
				
				if(!contacts.isEmpty())
				{
					returnFacultyStaffInfo.put(department.text(), contacts); 
				}					
			}
		}
		
		return returnFacultyStaffInfo;
	}

	private List<Link> scrapeResearchCenters(Document targetDocument) {
		String linkQuery = "h3";
		Elements reserchCenterElements = targetDocument.select(linkQuery);
		
		List<Link> returnResearchCenters = new ArrayList<Link>();
		
		for(Element h3 : reserchCenterElements){

			Elements link = h3.select("strong a");
			Element linkFirst = link.first();
			
			if(linkFirst != null)
			{
				Link researchCenter = WebScraperCommon.constructLinkGivenTagNonAbs(linkFirst); 
				
				if(researchCenter.url != "" && researchCenter.description != "")
				{
					returnResearchCenters.add(researchCenter);
				}
			}
		}
		
		return returnResearchCenters; 
	}

	private Map<String, List<Link>> scrapeAcademicDepartments(Document targetDocument) {
		Map<String, List<Link>> returnAcademicDepartment = new HashMap<String, List<Link>>();
		
		String linkQuery = "h3";
		Elements departments = targetDocument.select(linkQuery);

		for(Element department : departments){
			List<Link> degrees = new ArrayList<Link>();
			
			Elements ul = department.parent().children().select("ul");
			
			if (ul != null)
			{
				for(Element element : ul)
				{
					Elements selectDegrees = element.select("li a");
					
					for(Element degreeElement : selectDegrees)
					{
						Link degreeLink = WebScraperCommon.constructLinkGivenTag(degreeElement); 
						
						if(degreeLink.url != "")
							degrees.add(degreeLink); 
					}
				}
			}
			
			if(!degrees.isEmpty())
			{
				returnAcademicDepartment.put(department.text(), degrees); 
			}
		}
		
		return returnAcademicDepartment;
	}

	private List<String> scrapeAboutUs(Document targetDocument){
		List<String> aboutUsParagraphs = new ArrayList<String>();
		
		String linkQuery = "h1";
		Elements h1Headers = targetDocument.select(linkQuery);
		
		for(Element headerElement : h1Headers){
		
			if(headerElement.text().contains("About Us"))
			{
				Elements aboutParagraphs = headerElement.parent().children().select("p");
				
				if (aboutParagraphs != null)
				{
					for(Element paragraphElement : aboutParagraphs)
					{
						aboutUsParagraphs.add(paragraphElement.text());
					}
				}
			}
		}
		
		return aboutUsParagraphs; 
	}

	/**
	 * This method returns the WebData object. 
	 * 
	 * @return The WebData object associated with the current instance of WebScraper.
	 */
	public CollegeEngineeringWebData getScrapedData(){
		return this.webData;
	}
}
